from typing import Optional, List
from datetime import datetime

from pydantic import BaseModel, Field


# 资源基础模型
class ResourceBase(BaseModel):
    name: str
    description: Optional[str] = None
    resource_type: str
    availability: str = "available"
    cost_rate: Optional[float] = None
    max_units: float = 1.0
    skills: Optional[str] = None
    location: Optional[str] = None
    is_external: bool = False


# 创建资源
class ResourceCreate(ResourceBase):
    pass


# 更新资源
class ResourceUpdate(BaseModel):
    name: Optional[str] = None
    description: Optional[str] = None
    resource_type: Optional[str] = None
    availability: Optional[str] = None
    cost_rate: Optional[float] = None
    max_units: Optional[float] = None
    skills: Optional[str] = None
    location: Optional[str] = None
    is_external: Optional[bool] = None


# 资源分配基础模型
class ResourceAllocationBase(BaseModel):
    resource_id: int
    allocation_percentage: float = 100.0
    start_date: datetime
    end_date: datetime


# 创建项目资源分配
class ProjectResourceAllocationCreate(ResourceAllocationBase):
    project_id: int


# 创建任务资源分配
class TaskResourceAllocationCreate(ResourceAllocationBase):
    task_id: int


# 更新资源分配
class ResourceAllocationUpdate(BaseModel):
    allocation_percentage: Optional[float] = None
    start_date: Optional[datetime] = None
    end_date: Optional[datetime] = None


# API响应中的资源信息
class ResourceResponse(ResourceBase):
    id: int
    created_at: datetime
    updated_at: Optional[datetime] = None

    class Config:
        from_attributes = True


# API响应中的项目资源分配信息
class ProjectResourceAllocationResponse(ResourceAllocationBase):
    project_id: int

    class Config:
        from_attributes = True


# API响应中的任务资源分配信息
class TaskResourceAllocationResponse(ResourceAllocationBase):
    task_id: int

    class Config:
        from_attributes = True